#include "CLR_MD5.h"
#include "md5.h"

#define NULL 0

namespace CLR_MD5 {
    bool MD5::Init()
    {
        m_pMd5Context = new md5_context();
        if (!m_pMd5Context)
            return false;

        MD5_Init(m_pMd5Context);
        return true;
    }

    void MD5::UnInit()
    {
        if (m_pMd5Context)
        {
            delete m_pMd5Context;
            m_pMd5Context = NULL;
        }
    }

    array<unsigned char>^ MD5::Get(array<unsigned char>^inData)
    {
        if (inData == nullptr)
            throw gcnew ArgumentNullException("inData");

        pin_ptr<unsigned char> pInData = &inData[0];
        MD5_Update(m_pMd5Context, pInData, inData->Length);

        array<unsigned char>^ result = gcnew array<unsigned char>(16);
        pin_ptr<unsigned char> pOutData = &result[0];

        MD5_Finish(m_pMd5Context, pOutData);        
        return result;
    }

}